package org.digitarts.rtms.spring.collectors.shell;

import java.util.HashMap;
import java.util.Map;

import org.digitarts.rtms.spring.collectors.agent.jms.JMSAgentConstants;

public class DelegatingShellCollector extends ShellCollector
{
	@SuppressWarnings("unchecked")
	protected Map	context	= new HashMap();

	/**
	 * Extended to initialize context pass.
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void initCallback()
	{
		context.put(TRACING_NAMESPACE_KEY, this.tracingNameSpace);
		context.put(JMSAgentConstants.COMMAND_SET_BEAN_NAME, this.commandSet.getBeanName());
		super.initCallback();
	}

	/**
	 * Issues commands through an asynch shell where the results will be handled
	 * by a delegated result handler.
	 *
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void collectCallback() throws Exception
	{
		try
		{
			long start = System.currentTimeMillis();
			if (log.isDebugEnabled())
				log.debug("\n\t" + objectName + " Issuing Collection....");
			commandSet.processCommands(shell, context);
			long elapsed = System.currentTimeMillis() - start;
			if (log.isDebugEnabled())
				log.debug("\n\t" + objectName + " Collection Dispatched in " + elapsed + " ms.");
		}
		catch (Exception e)
		{
			if (logErrors)
			{
				log.error("Failed to collect [" + objectName + "]", e);
			}
		}

	}

}
